package com.kuanter.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kuanter.kuanterauto.HomeActivityGroup;
import com.kuanter.kuanterauto.R;
/**
 * home界面选择tab适配器
 * @author soup
 *
 */
public class HomeGroupAdapter extends BaseAdapter {

	private Context mContext;
	private RelativeLayout[] rLayout;
	private static LayoutInflater mLayoutInflater;
	static int width;
	static int height;
	String[] navigationArrays;

	// public HomeGroupAdapter(Context context, int[] picIds, int width, int
	// height) {
	public HomeGroupAdapter(Context context, int[] picIds, int width, int height) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(mContext);
		navigationArrays = mContext.getResources().getStringArray(
				R.array.NavigationNames);
		this.height = height;
		this.width = width;
		rLayout = new RelativeLayout[picIds.length];
		for (int i = 0; i < picIds.length; i++) {
			rLayout[i] = new BottomLayout(mContext, picIds[i],
					navigationArrays[i]);
			rLayout[i]
					.setLayoutParams(new GridView.LayoutParams(width, height));
		}
	}

	public int getCount() {
		return rLayout.length;
	}

	public Object getItem(int position) {
		return rLayout[position];
	}

	public long getItemId(int position) {
		return position;
	}

	public void clearFocus() {
		for (int i = 0; i < rLayout.length; i++) {
			// rLayout[i].setBackgroundResource(0);// 恢复未选中的样式
			ImageView homeImage = (ImageView) rLayout[i]
					.findViewById(R.id.home_image);
			homeImage.setBackgroundResource(0);
		}
	}

	public void setJumpFocus(int index) {
		// rLayout[index].setBackgroundResource(HomeActivityGroup.topbar_image_array[index]);
		ImageView homeImage = (ImageView) rLayout[index]
				.findViewById(R.id.home_image);
		homeImage
				.setBackgroundResource(HomeActivityGroup.topbar_image_array[index]);

	}

	public void showReaddian() {
		ImageView iv_readdian = (ImageView) rLayout[3]
				.findViewById(R.id.iv_readdian);
		iv_readdian.setVisibility(View.VISIBLE);
	}

	public void goneReaddian() {
		ImageView iv_readdian = (ImageView) rLayout[3]
				.findViewById(R.id.iv_readdian);
		iv_readdian.setVisibility(View.GONE);
	}

	/**
	 * 设置选中的效果
	 */
	public void setFocus(int index) {
		clearFocus();
		setJumpFocus(index);
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		RelativeLayout rView;
		if (convertView == null) {
			rView = rLayout[position];
		} else {
			rView = (RelativeLayout) convertView;
		}

		return rView;
	}

	static class BottomLayout extends RelativeLayout {
		RelativeLayout load;
		ImageView homeImage;
		ImageView iv_readdian;
		TextView bottomText;
		int picid;
		String str;

		public BottomLayout(Context context, int picid, String str) {

			super(context);
			this.picid = picid;
			this.str = str;
			load = (RelativeLayout) mLayoutInflater.inflate(R.layout.home_item,
					null);
			homeImage = (ImageView) load.findViewById(R.id.home_image);
			iv_readdian = (ImageView) load.findViewById(R.id.iv_readdian);
			// bottomText.setText(str);
			// if (picid == R.drawable.icon_more) {
			// CartHandler run = new CartHandler(my_unread_msg);
			// new Thread(run).start();
			// setCartIcon();
			// }
			// homeImage.setImageResource(picid);
			// homeImage.setPadding(4,4, 4, 4);
			// homeImage.setAdjustViewBounds(false);
			this.addView(load, width, height);
			// refresh(this);
		}

		public void setCartIcon() {
			// my_unread_msg.setText(ProApplication.msgCount + "");
			// if (ProApplication.msgCount >= 10) {
			// my_unread_msg.setTextSize(10);
			// }
			// my_unread_msg.setVisibility(View.GONE);
			// LayoutParams LMyImage = (LayoutParams) my_unread_msg
			// .getLayoutParams();
			// LMyImage.leftMargin = width / 2;
			// LMyImage.topMargin = 0;
			// my_unread_msg.setLayoutParams(LMyImage);
		}
	}

	public static class CartHandler implements Runnable {
		private TextView tv;
		private Handler hr;

		public CartHandler(TextView tv) {
			this.tv = tv;
			hr = new Handler() {
				@Override
				public void handleMessage(Message msg) {
					// CartHandler.this.tv.setText(ProApplication.msgCount +
					// "");
					// if (ProApplication.msgCount >= 10) {
					// CartHandler.this.tv.setTextSize(10);
					// }
					// if (ProApplication.msgCount > 0) {
					// CartHandler.this.tv.setVisibility(View.VISIBLE);
					// } else {
					// CartHandler.this.tv.setVisibility(View.GONE);
					// }
				}
			};
		}

		public void run() {
			int msg = 0;
			// while (true) {

			// if (msg != ProApplication.msgCount) {
			hr.sendMessage(new Message());
			// }
			// msg = ProApplication.msgCount;
			// }
		}

	}

}
